package com.cy.dda.web.front.customer;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.customer.CustomerAttachmentAddAO;
import com.cy.dda.model.ao.customer.CustomerCompanyInfoEditAO;
import com.cy.dda.model.ao.customer.CustomerContactInfoEditAO;
import com.cy.dda.model.ao.customer.CustomerInvestigationDescAddAO;
import com.cy.dda.model.ao.customer.CustomerPersonalInfoEditAO;
import com.cy.dda.model.domain.customer.CustomerAttachment;
import com.cy.dda.model.domain.customer.CustomerInvestigationDesc;
import com.cy.dda.model.query.customer.CustomerListQuery;
import com.cy.dda.model.query.org.CustomerOrgListQuery;
import com.cy.dda.model.vo.customer.CustomerAttachmentVO;
import com.cy.dda.model.vo.customer.CustomerCompanyInfoVO;
import com.cy.dda.model.vo.customer.CustomerContactInfoVO;
import com.cy.dda.model.vo.customer.CustomerDetailInfoVO;
import com.cy.dda.model.vo.customer.CustomerListVO;
import com.cy.dda.model.vo.customer.CustomerPersonalDataVO;
import com.cy.dda.model.vo.customer.CustomerPersonalInfoVO;
import com.cy.dda.model.vo.org.CustomerOrgListVO;
import com.cy.dda.service.customer.CustomerService;
import com.cy.dda.service.org.OrgService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 前台-客户
 *
 * @author: tanyilin
 * @create: 2018年11月14日 下午3:18:28
 */
@Slf4j
@RestController
@RequestMapping("/user/customer")
@Api(tags = "前台-客户")
public class CustomerController extends BaseController {

	@Autowired
	private CustomerService customerService;
	@Autowired
	private OrgService orgService;
	
	@ApiOperation("获取我的客户列表")
	@GetMapping("/list")
	public PageBean<CustomerListVO> list(CustomerListQuery query) {
		Long userId = this.getUser().getUserId();
		query.setUserId(userId);
		PageBean<CustomerListVO> pageBean = customerService.selectCustomerList(query);
		return pageBean;
	}
	
	@ApiOperation("获取客户详情")
	@GetMapping("/get/detail")
	public Result<CustomerDetailInfoVO> detail(@RequestParam Long customerId) {
		CustomerDetailInfoVO customerDetailInfoVO = customerService.selectCustomerDetailInfoVO(customerId);
		return ok("", customerDetailInfoVO);
	}
	
	@ApiOperation("获取更多客户个人资料")
	@GetMapping("/get/personal/data")
	public Result<CustomerPersonalDataVO> personalData(@RequestParam Long customerId) {
		CustomerPersonalDataVO personalData = customerService.selectCustomerPersonalDataVO(customerId);
		return ok("", personalData);
	}
	
	@ApiOperation("获取客户附件（图片、视频、音频）资料")
	@GetMapping("/get/attachments")
	public Result<List<CustomerAttachmentVO>> attachments(@RequestParam Long customerId) {
		List<CustomerAttachmentVO> list = customerService.selectCustomerAttachmentList(customerId);
		return ok("", list);
	}
	
	@ApiOperation("新增客户附件资料")
	@PostMapping("/do/add/attachment")
	public Result doAddAttachment(@Validated CustomerAttachmentAddAO addAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		CustomerAttachment customerAttachment = new CustomerAttachment();
		BeanUtils.copyProperties(addAO, customerAttachment);
		customerService.addAttachment(customerAttachment);
		return ok(); 
	}
	
	@ApiOperation("获取客户个人信息")
	@GetMapping("/get/personal/info")
	public Result<CustomerPersonalInfoVO> personalInfo(@RequestParam Long customerId) {
		CustomerPersonalInfoVO customerPersonalInfoVO = customerService.selectCustomerPersonalInfo(customerId);
		return ok("", customerPersonalInfoVO);
	}
	
	@ApiOperation("获取客户联系人信息")
	@GetMapping("/get/contact/info")
	public Result<List<CustomerContactInfoVO>> contactInfo(@RequestParam Long customerId) {
		List<CustomerContactInfoVO> list = customerService.selectCustomerContactInfo(customerId);
		return ok("", list);
	}
	
	@ApiOperation("获取客户单位信息")
	@GetMapping("/get/company/info")
	public Result<CustomerCompanyInfoVO> companyInfo(@RequestParam Long customerId) {
		CustomerCompanyInfoVO customerCompanyInfoVO = customerService.selectCustomerCompanyInfoVO(customerId);
		return ok("", customerCompanyInfoVO);
	}
	
	@ApiOperation("更新客户个人信息")
	@PostMapping("/update/personal/info")
	public Result updatePersonalInfo(@Validated CustomerPersonalInfoEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		customerService.updateCustomerPersonalInfo(editAO, this.getUser().getUserId());
		return ok();
	}
	
	@ApiOperation("更新客户联系人信息")
	@PostMapping("/update/contact/info")
	public Result updateContactInfo(@Validated @RequestBody CustomerContactInfoEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		customerService.updateCustomerContactInfo(editAO);
		return ok();
	}
	
	@ApiOperation("更新客户单位信息")
	@PostMapping("/update/company/info")
	public Result updateCompanyInfo(@Validated CustomerCompanyInfoEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		customerService.updateCustomerCompanyInfo(editAO, this.getUser().getUserId());
		return ok();
	} 
	
	@ApiOperation("新增客户调查说明")
	@PostMapping("/do/add/investigation/desc")
	public Result doAddInvestigationDesc(@Validated CustomerInvestigationDescAddAO addAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		CustomerInvestigationDesc customerInvestigationDesc = new CustomerInvestigationDesc();
		BeanUtils.copyProperties(addAO, customerInvestigationDesc);
		Date time = new Date();
		customerInvestigationDesc.setUserId(this.getUser().getUserId());
		customerInvestigationDesc.setCreateTime(time);
		customerInvestigationDesc.setUpdateTime(time);
		customerService.addCustomerInvestigationDesc(customerInvestigationDesc);
		return ok();
	}
	
	@ApiOperation("获取客户的委案机构列表")
	@GetMapping("/get/org/list")
	public PageBean<CustomerOrgListVO> customerOrgList(CustomerOrgListQuery query) {
		PageBean<CustomerOrgListVO> pageBean = orgService.selectCustomerOrgList(query);
		return pageBean;
	}
	
	
}
